/**
 * Mail Contact address
 * 
 * @author		Tran Anh Tuan <itanium7000@gmail.com> or <tuan.tran@gen.vn>
 * 
 */
var MAIL_CONTACT = {
	
	v_fShowForm : function(id, title, width, height, callbackOK, callbackCancel)
	{
		COREJS.initDialog(id, width, height);
		$('#'+id).dialog('option', 'title', title);
		$('#'+id).dialog('option', 'buttons', { 
			"Hủy bỏ" : function() {
				if (callbackCancel) {
					callbackCancel();
				}
				$(this).dialog('close');
			},
			"Lưu lại" : function() {
				if (callbackOK) {
					callbackOK();
				}
			},			
			
		});		
		$('#'+id).dialog('open');			
	},
	/**
	 * Create contact address
	 * 
	 * @author		Tran Anh Tuan <itanium7000@gmail.com> or <tuan.tran@gen.vn>
	 * @param		none
	 * @return		void
	 */
	v_fCreate : function()
	{
		iBookId = $('#mail_cta_book_id').val();
		MAIL_CONTACT.v_fShowForm('mail_cta_frm', COREJS._getJsLang('CREATE_ADDRESS'), 400, 160,
			function(){
				// Create contact address
				sz_Name = $('#cta-contact-name').val();
				sz_Email = $('#cta-contact-email').val();
				sz_Title = $('#cta-contact-title').val();
				if ('' == sz_Name) {
					COREJS.v_fShowOKDialog(COREJS._getJsLang('NAME_NOT_EMPTY'), function(){
						$('#cta-contact-name').focus().fadeOut(200).fadeIn(200).fadeOut(200).fadeIn(200);
					});
				}	
				else
				{
					url = 'them_dia_chi.php';
					data = '{"sz_Name":"'+sz_Name+'","sz_Email":"'+sz_Email+'","sz_Title":"'+sz_Title+'","i_BookId":"'+iBookId+'"}';
					COREJS.v_fGetJson(url, JSON.parse(data), function(response){
						if (0 == response.e) {
							if (1 == response.validated)
							{
								if (response.exists)
								{
									COREJS.v_fShowYesNoDialog(COREJS._getJsLang('_MAIL_ADDRESS_NAME_EXIST'),
										function(){
											url =  'overwrite.php';
											data = '{"sz_Name":"'+sz_Name+'","sz_Email":"'+sz_Email+'","sz_Title":"'+sz_Title+'","iContactId":'+response.exists+'}';
											COREJS.v_fGetJson(url, JSON.parse(data), function(res){
												if (0 == res.e)
												{
													COREJS.v_fShowOKDialog(COREJS._getJsLang('_MAIL_UPDATED_CTA'), function(){
														$('#mail_cta_frm').dialog('close');
														$('#ADD_Name_display_' + response.exists + ' a').text(sz_Name);
														$('#ADD_mail_address_' + response.exists).text(sz_Email);
														$('#ADD_Contact_Title_' + response.exists).text(sz_Title);
														$('#listview_CONTACT_ADDRESS_row_' + response.exists + ' td').fadeOut(200).fadeIn(200).fadeOut(200).fadeIn(200);
													});
												}
												else {
													COREJS.v_fShowOKDialog(res.e);
												}
											});
										},
										function(){
											$('#cta-contact-name').val(sz_Name).focus();
										}
									);
								}
								else
								{
									COREJS.v_fShowOKDialog(COREJS._getJsLang('_MAIL_ADDRESSES_CREATED'), function(){
										$(window.location).attr('href', 'index.php?tn=email_danhba_detail&book='+iBookId);
										$('#mail_cta_frm').dialog('close');
									});
								}
							}
							else {
								COREJS.v_fShowOKDialog(COREJS._getJsLang('EMAIL_FAID'), function(){
									$('#cta-contact-email').focus().fadeOut(200).fadeIn(200).fadeOut(200).fadeIn(200);
								});
							}
						}
						else {
							COREJS.v_fShowOKDialog(response.e);
						}						
					});					
				}
			},
			function(){
				// Cancel
			}			
		);
		$('#cta-contact-email').val('');
		$('#cta-contact-name').val('').focus();
		// change text of buttons
		if ($('.ui-dialog-buttonpane button:nth-child(1)').text() == 'OK') {
			$('.ui-dialog-buttonpane button:nth-child(1)').text('Lưu lại');
		}
		if ($('.ui-dialog-buttonpane button:nth-child(2)').text() == 'Cancel') {
			$('.ui-dialog-buttonpane button:nth-child(2)').text('Hủy bỏ');
			$('.ui-dialog-buttonpane button').css('color', '#FFF');
			$('.ui-dialog-buttonpane button').css('width', '65px');
			$('.ui-dialog-buttonpane button').css('font-size', '12px');
			$('.ui-dialog-buttonpane button').css('padding', '0.1em 0.6em 0.1em');
			$('.ui-dialog-buttonpane button:nth-child(1)').css('background', '-moz-linear-gradient(center top , #00ADEE, #0078A5) repeat scroll 0 0 transparent');
			$('.ui-dialog-buttonpane button:nth-child(2)').css('background', '-moz-linear-gradient(center top , #FAA51A, #F47A20) repeat scroll 0 0 transparent');			
		}		
	},
	
	/**
	 * Edit mail contact address
	 * 
	 * @author		Tran Anh Tuan <itanium7000@gmail.com> or <tuan.tran@gen.vn>
	 * @param		integer
	 * @return		void
	 */
	v_fEdit : function(the_Id)
	{
		iBookId = $('#mail_cta_book_id').val();
		sz_Name = $('#ADD_Name_display_'+the_Id+' a').text();
		sz_Mail = $('#ADD_mail_address_'+the_Id).text();
		sz_Title = $('#ADD_Contact_Title_'+the_Id).text();
		MAIL_CONTACT.v_fShowForm('mail_cta_frm', COREJS._getJsLang('_MAIL_EDIT_CTA'), 400, 160,
			function(){
				// Update
				sz_Name = $('#cta-contact-name').val();
				sz_Email = $('#cta-contact-email').val();
				sz_Title = $('#cta-contact-title').val();
				if ('' == sz_Name) {
					COREJS.v_fShowOKDialog(COREJS._getJsLang('NAME_NOT_EMPTY'), function(){
						$('#cta-contact-name').focus().fadeOut(200).fadeIn(200).fadeOut(200).fadeIn(200);
					});
				}	
				else
				{
					url = 'update_diachi.php';
					data = '{"sz_Name":"'+sz_Name+'","sz_Email":"'+sz_Email+'","sz_Title":"'+sz_Title+'","iContactId":'+the_Id+',"i_BookId":'+iBookId+'}';
					COREJS.v_fGetJson(url, JSON.parse(data), function(response){
						if (0 == response.e) {
							if (1 == response.validated)
							{
								
								
								
									COREJS.v_fShowOKDialog(COREJS._getJsLang('_MAIL_UPDATED_CTA'), function(){
										//$(window.location).attr('href', COREJS._getBaseUrl() + '/modules/mail/contact/?book='+iBookId);
										$('#mail_cta_frm').dialog('close');
										$('#ADD_Name_display_' + the_Id + ' a').text(sz_Name);
										if ('0' == sz_Title) {
											sz_Title = '';
										}
										$('#ADD_Name_display_' + the_Id + ' sup').text(sz_Title);
										$('#ADD_mail_address_' + the_Id).text(sz_Email);
										$('#ADD_last_update_' + the_Id).text(response.d);
										$('#listview_CONTACT_ADDRESS_row_' + the_Id + ' td').fadeOut(200).fadeIn(200).fadeOut(200).fadeIn(200);
									});
								
							}
							else {
								COREJS.v_fShowOKDialog(COREJS._getJsLang('EMAIL_FAID'), function(){
									$('#cta-contact-email').focus().fadeOut(200).fadeIn(200).fadeOut(200).fadeIn(200);
								});
							}
						}
						else {
							COREJS.v_fShowOKDialog(response.e);
						}						
					});					
				}				
			},
			function(){
				// Cancel
			}			
		);
		$('#cta-contact-email').val(sz_Mail);
		$('#cta-contact-title').val(sz_Title);
		$('#cta-contact-name').focus().val(sz_Name);
		COREJS.v_fChangeButtons();
	},
	
	/**
	 * Delete selected contact address
	 * 
	 * @author		Tran Anh Tuan <itanium7000@gmail.com> or <tuan.tran@gen.vn>
	 * @param		none
	 * @return		void
	 */	
	v_fDeleteSelected : function()
	{
		sz_Selected = MAIL_CONTACT.v_fCheckSelected();
		iBookId = $('#mail_cta_book_id').val();
		if (false == sz_Selected){
			COREJS.v_fShowOKDialog(COREJS._getJsLang('_MAIL_CTA_DELETE_EMPTY'));		
		}	
		else
		{
			COREJS.v_fShowYesNoDialog(COREJS._getJsLang('_MAIL_DELETED_CTA_CONFIRM'),
				function(){
					// Delete
					url = 'xoa_diachi.php?ids=' + sz_Selected;
					COREJS.v_fGetJson(url, null, function(response){
						if (0 == response.e)
						{
							COREJS.v_fShowOKDialog(COREJS._getJsLang('_MAIL_DELETED')+' <b>'+response.n+'</b> '+COREJS._getJsLang('_MAIL_DELETED_CTA_ADDRESS_NUM'),
								function(){
									$(window.location).attr('href','index.php?tn=email_danhba_detail&book='+iBookId);
								}		
							);
						}
						else
						{
							COREJS.v_fShowOKDialog('Error: '+response.e);
						}
					});
				},
				function(){}
			);
		}
	},
	
	/**
	 * Directly delete one contact address
	 * 
	 * @author		Tran Anh Tuan <itanium7000@gmail.com> or <tuan.tran@gen.vn>
	 * @param		none
	 * @return		void
	 */
	v_fDirectDelete : function(the_Id)
	{
		COREJS.v_fShowYesNoDialog(COREJS._getJsLang('_MAIL_DELETED_CTA_CONFIRM'),
			function(){
				// Delete
				url = 'delete_diachi.php?id=' + the_Id;
				COREJS.v_fGetJson(url, null, function(response){
					if (0 == response.e)
					{
						iBookId = $('#mail_cta_book_id').val();
						COREJS.v_fShowOKDialog(COREJS._getJsLang('_MAIL_DELETED')+' <b>'+response.n+'</b> '+COREJS._getJsLang('_MAIL_DELETED_CTA_ADDRESS_NUM'),
							function(){
								$(window.location).attr('href','index.php?tn=email_danhba_detail&book='+iBookId);
							}	
						);
					}
					else {
						COREJS.v_fShowOKDialog('Error: ' + response.e);
					}
				});
			},
			function(){}
		);
	},
	
	/**
	 * Check for have not selected any row
	 * 
	 * @author	Tran Anh Tuan <tuan.tran@gen.vn>
	 * @param	none
	 * @return	boolean
	 */
	v_fCheckSelected : function()
	{
		list = '';
        $("table.listview tbody tr td:first-child input:checkbox").each(function(index) {
        	if(this.checked){
        		id = this.id.match(/\d+$/)[0];
        		list += id + ',';
        	}
        });		
        if(list == '') {
        	return false;
        }
        return list;	
	},
	
	/**
	 * Direct send mail to contact address
	 * 
	 * @author		Tran Anh Tuan <itanium7000@gmail.com> or <tuan.tran@gen.vn>
	 * @param		none
	 * @return		void
	 */
	v_fDirectSend : function (the_Id)
	{
		$(window.location).attr('href', COREJS._getBaseUrl() + '/modules/mail/compose/?address='+the_Id);		
	},
	
	/**
	 * Send mail to selected contacts
	 * 
	 * @author		Tran Anh Tuan <itanium7000@gmail.com> or <tuan.tran@gen.vn>
	 * @param		none
	 * @return		void
	 */
	v_fSendSelected : function()
	{
		sz_Selected = MAIL_CONTACT.v_fCheckSelected();
		if (false == sz_Selected){
			COREJS.v_fShowOKDialog(COREJS._getJsLang('_MAIL_CTA_SEND_EMPTY'));		
		}
		else
		{
			$(window.location).attr('href', COREJS._getBaseUrl() + '/modules/mail/compose/?address='+sz_Selected);
		}
	},
	
	/**
	 * Show importation file popup
	 * 
	 * @author		Tran Anh Tuan <itanium7000@gmail.com> or <tuan.tran@gen.vn>
	 * @param		ingteger
	 * @return		void
	 */
	v_fShowImportFile : function(the_Type)
	{
		$('#btn-start-import').attr('disabled', false);
		$('#userfile').attr('disabled', false);
		$('#import-option-update').attr('disabled', false);
		$('#import-option-replace').attr('disabled', false);
		$('#btn-start-import').attr('value', COREJS._getJsLang('_START_IMPORT'));
		sz_Title = COREJS._getJsLang('_MAIL_CTA_IMPORT') + ' - ' + $('#Mail_Cta_Import_Drp_dropdown li:nth-child(' + the_Type + ')').text();
		COREJS.v_fShowFromPopupWithButton('import_popup', sz_Title, 600, 400, function(){
			$('#import_popup').dialog('close');
			if ($('#btn-start-import').attr('disabled'))
			{
				location.reload();
			}
		});
		$('#btn-start-import').click(function(){
			if ('' == $('#userfile').val())
			{
				COREJS.v_fShowOKDialog(COREJS._getJsLang('_IMPORT_FILE_CANNOT_EMPTY'), function(){
					$('#userfile').focus();
				});
			}
			else {
				$('#btn-start-import').attr('disabled', true);
				$('#userfile').attr('readonly', true);
				$('#btn-start-import').attr('value', COREJS._getJsLang('_IMPORTING'));
				$('<div class="import_popup_result"></div>').insertAfter('#import_popup ul:nth-child(1)');
				$('#import_popup .import_popup_result').html(COREJS._getJsLang('_IMPORTING')+'<br /><img src="images/importing.gif"/>');
				$('#import_popup .import_popup_result').css('text-align', 'center');				
			}
		});
	},
	
	/**
	 * Show popup of importation contact address from customer
	 * 
	 * @author		Tran Anh Tuan <itanium7000@gmail.com> or <tuan.tran@gen.vn>
	 * @param		integer
	 * @return		void
	 */
	v_fShowImportFromCustomer : function(the_Type)
	{
		$('#btn-start-import-c').attr('enabled');
		$('#btn-start-import-c').attr('value', COREJS._getJsLang('_START_IMPORT'));
		sz_Title = COREJS._getJsLang('_MAIL_CTA_IMPORT') + ' - ' + $('#Mail_Cta_Import_Drp_dropdown li:nth-child(' + the_Type + ')').text();
		COREJS.v_fShowFromPopupWithButton('import_popup_customer', sz_Title, 600, 500,
			function(){
				$('#import_popup_customer').dialog('close');
				if ($('#btn-start-import-c').attr('disabled'))
				{
					location.reload();
				}			
			}
		);
		$('.import_popup_result').html('');
		$('#btn-start-import-c').click(function(){
			sz_Selected = '';  
			$(".sl-l-body .sl-l-row div:nth-child(1) span input:checkbox").each(function() {
				if (this.checked) {
					sz_Selected += ',' + this.value;
				}
			});

			urlSend = COREJS._getBaseUrl() + '/modules/mail/contact/importFromCustomer';
			$.ajax({
				url: urlSend,
				type: "POST",
				data: ({
					bookId: parseInt($('#mail_cta_book_id').val()),
					isAdd: $('#import-option-add-c').attr("checked"),
					isUpdate: $('#import-option-update-c').attr("checked"),
					isCaNhan: $('#c_sl_list').val(),
					ids: sz_Selected,
					cType: $('#c_sl_list').val(),
					partName: $('#sl_s_email_keyword').val()
				}),
				dataType: 'json',
				beforeSend: function(){
					$('#import_popup_customer .import_popup_result').html(COREJS._getJsLang('_IMPORTING')+'<br /><img src="'+COREJS._getBaseUrl()+'/images/icons/importing.gif"/>');
					$('#import_popup_customer .import_popup_result').css('text-align', 'center');			
					$('#btn-start-import-c').attr('disabled', true);
					$('#btn-start-import-c').attr('value', COREJS._getJsLang('_IMPORTING'));
				},
				success: function(returnInfos){
					$('#btn-start-import-c').attr('value', COREJS._getJsLang('_IMPORT_COMPLETE'));
					$('.import_popup_result').html(returnInfos.msg);
				}
			});
			/*
			$('#import_popup_customer .import_popup_result').html(COREJS._getJsLang('_IMPORTING')+'<br /><img src="'+COREJS._getBaseUrl()+'/images/icons/importing.gif"/>');
			$('#import_popup_customer .import_popup_result').css('text-align', 'center');			
			$('#btn-start-import-c').attr('disabled', true);
			$('#btn-start-import-c').attr('value', COREJS._getJsLang('_IMPORTING'));
			*/
			
			/*
			$('#import_popup_customer .import_popup_result').html(COREJS._getJsLang('_IMPORTING')+'<br /><img src="'+COREJS._getBaseUrl()+'/images/icons/importing.gif"/>');
			$('#import_popup_customer .import_popup_result').css('text-align', 'center');			
			$('#btn-start-import-c').attr('disabled', true);
			$('#btn-start-import-c').attr('value', COREJS._getJsLang('_IMPORTING'));
			
			url = COREJS._getBaseUrl() + '/modules/mail/contact/importFromCustomer/?book=' + iBookId;
			COREJS.v_fGetJson(url, null, function(response){
				$('#btn-start-import-c').attr('value', COREJS._getJsLang('_IMPORT_COMPLETE'));
				$('.import_popup_result').html(response.html);
			});
			*/
		});
	},
	
	v_fShowImportFromCustomerContact : function(the_Type)
	{
		$('#btn-start-import-cc').attr('enabled');
		$('#btn-start-import-cc').attr('value', COREJS._getJsLang('_START_IMPORT'));
		iBookId = $('#mail_cta_book_id').val();
		sz_Title = COREJS._getJsLang('_MAIL_CTA_IMPORT') + ' - ' + $('#Mail_Cta_Import_Drp_dropdown li:nth-child(' + the_Type + ')').text();
		COREJS.v_fShowFromPopupWithButton('import_popup_customer_contact', sz_Title, 600, 500,
			function(){
				$('#import_popup_customer_contact').dialog('close');
				if ($('#btn-start-import-cc').attr('disabled'))
				{
					location.reload();
				}			
			}
		);
		$('import_popup_customer_contact .import_popup_result').html('');
		$('#btn-start-import-cc').click(function(){
			$('#import_popup_customer_contact .import_popup_result').html(COREJS._getJsLang('_IMPORTING')+'<br /><img src="'+COREJS._getBaseUrl()+'/images/icons/importing.gif"/>');
			$('#import_popup_customer_contact .import_popup_result').css('text-align', 'center');
			$('#btn-start-import-cc').attr('disabled', true);
			$('#btn-start-import-cc').attr('value', COREJS._getJsLang('_IMPORTING'));
			
			url = COREJS._getBaseUrl() + '/modules/mail/contact/importFromCustomerContact/?book=' + iBookId;
			COREJS.v_fGetJson(url, null, function(response){
				$('#btn-start-import-cc').attr('value', COREJS._getJsLang('_IMPORT_COMPLETE'));
				$('#import_popup_customer_contact .import_popup_result').html(response.html);
			});
		});	
	},
	
	/**
	 * Importation
	 * 
	 * @author		Tran Anh Tuan <itanium7000@gmail.com> or <tuan.tran@gen.vn>
	 * @param		none
	 * @return		void
	 */
	v_fImport : function (the_Type)
	{
		switch(the_Type)
		{
			case '1':
				MAIL_CONTACT.v_fShowImportFile(the_Type);
				break;
			case '2':
				MAIL_CONTACT.v_fShowImportFromCustomer(the_Type);
				break;	
			case '3':
				MAIL_CONTACT.v_fShowImportFromCustomerContact(the_Type);
				break;					
		}
	},
	
	/**
	 * Export data to file as importable data file
	 * 
	 * @author		Tran Anh Tuan <itanium7000@gmail.com> or <tuan.tran@gen.vn>
	 * @param		none
	 * @return		void
	 */
	v_fExport : function()
	{
		iBookId = $('#mail_cta_book_id').val();
		sz_Selected = MAIL_CONTACT.v_fCheckSelected();
		if (false == sz_Selected)
		{
			COREJS.v_fShowYesNoDialog(COREJS._getJsLang('_EXPORT_NOT_SELECTED'),
				function(){
					url = COREJS._getBaseUrl() + '/modules/mail/contact/export/?book=' + iBookId;
					window.location.href = url;
				},
				function(){}
			);
		}
		else
		{
			url = COREJS._getBaseUrl() + '/modules/mail/contact/exportSelected/?contacts=' + sz_Selected;
			window.location.href = url;
		}
	},
	
	v_fSearch : function()
	{
		if ($('#text_toolbar_CONTACT_ADDRESS_search').val().length > 0)
		{
			url = COREJS._getBaseUrl() + '/modules/mail/contact/?book=' + $('#mail_cta_book_id').val() + '&search=' + $('#text_toolbar_CONTACT_ADDRESS_search').val();
			window.location.href = url;
		}
		else
		{
			$('#text_toolbar_CONTACT_ADDRESS_search').focus();
		}
	},
	
    scrolify : function(tblAsJQueryObject, height)
    {
    	/*
        var oTbl = tblAsJQueryObject;

        // for very large tables you can remove the four lines below
        // and wrap the table with <div> in the mark-up and assign
        // height and overflow property  
        var oTblDiv = $("<div/>");
        oTblDiv.css('height', height);
        oTblDiv.css('overflow','auto');               
        oTbl.wrap(oTblDiv);

        // save original width
        oTbl.attr("data-item-original-width", oTbl.width());
        oTbl.find('thead tr th').each(function(){
            $(this).attr("data-item-original-width",$(this).width());
        }); 
        oTbl.find('tbody tr:eq(0) td').each(function(){
            //$(this).attr("data-item-original-width",$(this).width());
        	$(this).attr("data-item-original-width", oTbl.find('thead tr th').attr("data-item-original-width"));
        });                 


        // clone the original table
        var newTbl = oTbl.clone();

        // remove table header from original table
        oTbl.find('thead tr').remove();                 
        // remove table body from new table
        newTbl.find('tbody tr').remove();   

        oTbl.parent().parent().prepend(newTbl);
        newTbl.wrap("<div/>");

        // replace ORIGINAL COLUMN width                
        newTbl.width(newTbl.attr('data-item-original-width'));
        newTbl.find('thead tr th').each(function(){
            $(this).width($(this).attr("data-item-original-width"+20));
        });     
        oTbl.width(oTbl.attr('data-item-original-width'));      
        oTbl.find('tbody tr:eq(0) td').each(function(){
            $(this).width($(this).attr("data-item-original-width"));
        });      
        */           
    },
    /**
     * search email khach hang
     * @author: VU Anh Tuan <tuan.vu@gen.vn>
     * @version: 1.0
     * @since 2012-08-09
     */
    v_fDoSearchEmail : function()
	{
		$i_Option = $('#c_sl_list').val();
		$i_Option = parseInt($i_Option);
		  
		// Search from customers
		var urlSend;
		switch ($i_Option){
		  	case 0:
		  		urlSend = COREJS._getBaseUrl()+'/modules/mail/contact/loadEmailOrgCust';
		  		break;
		  	case 1:
		  		urlSend = COREJS._getBaseUrl()+'/modules/mail/contact/loademailsbyInviduals';
		  		break;
		  	case 2:
		  		urlSend = COREJS._getBaseUrl()+'/modules/mail/contact/loademailsbyContactentry';
		  		break;
		}
		$.ajax({
			url: urlSend,
			type: "POST",
			data: ({
				partName: $('#sl_s_email_keyword').val()
			}),
			dataType: 'json',
			beforeSend: function(){
			},
			success: function(searchCustomers){
		  		if (searchCustomers.error == true){
		  			alert(searchCustomers.msg);
		  		} else{
		  			$('#sl_list_email .sl-l-body').empty();
		  			var icount = 0;
		  			$.each(searchCustomers.khachHangInfos, function(index, item) {
			 			$('#sl_list_email .sl-l-body').append('<div class="sl-l-row"><div><span><input type="checkbox" value="' + item.id + '" /></span></div><div><span>'+item.tenKh+'</span></div><div><span>'+item.email+'</span></div><div><span>'+item.capNhatCuoi+'</span></div></div>');
			  			icount++;
		  			});
		  			$('#ft_total_num').html('<b>'+icount + '</b> ' + COREJS._getJsLang('MAIL_ADD'));
		  		}
			}
		});
		$('#sl_list_email').show();
	}

}

/* Document ready */
$(document).ready(function() { 
	// uncheck all selectable 
    $("table.listview tbody tr td:first-child input:checkbox").each(function() {
        this.checked = false;
    });
	
	// Togle selecable
    $("table.listview thead tr th:first input:checkbox").click(function() {
        var checkedStatus = this.checked;
        $("table.listview tbody tr td:first-child input:checkbox").each(function() {
            this.checked = checkedStatus;
        });
    });  
    
    
    // Loop over each hottie.
    $("table.listview tbody tr td:first-child input:checkbox" ).each(
	    function(intIndex){
		    $(this).bind (
		    "click",
		    function(){
		    		//alert( "Hottie index: " + intIndex );
		    }
		    );
	});  
    MAIL_CONTACT.scrolify($('#listview_CONTACT_ADDRESS'), 400);
    // Autofocus on search box
    $('#text_toolbar_CONTACT_ADDRESS_search').focus();  
    
	$("#text_toolbar_CONTACT_ADDRESS_search").keyup(function(event){
		  if(event.keyCode == 13){
			  MAIL_CONTACT.v_fSearch();
		  }
	});

	/**
	 * added by tuan.vu
	 * script for search khach hang
	 */
	// Search button clicked
	$('#do_search').click(function(){
		MAIL_CONTACT.v_fDoSearchEmail();
	});
	  
	// Check for display button of clear search box
	$('#sl_s_email_keyword').change(function(){
		if ($('#sl_s_email_keyword').val().length > 0) {
			$('#do_search_clr').show();
		}
		else {
			$('#do_search_clr').hide();
		}
	});
	$('#c_sl_list').change(function(){
		$('#sl_list_email .sl-l-body').empty();
	});
	$('#sl_s_email_keyword').keypress(function(){
		if ($('#sl_s_email_keyword').val().length > 0) {
			$('#do_search_clr').show();
		}
		else {
			$('#do_search_clr').hide();
		}
	});	  
	// Clear search box
	$('#do_search_clr').click(function(){
		$('#sl_s_email_keyword').val('').focus();
		$('#do_search_clr').hide();
	});
	// check box status
	$(".sl-l-header div:first span input:checkbox").click(function() {
		var checkedStatus = this.checked;
		$(".sl-l-body .sl-l-row div:nth-child(1) span input:checkbox").each(function() {
		    this.checked = checkedStatus;
		});
	});	
	// end script for search khach hang

	/**
	 * added by tuan.vu
	 * script for search dmll
	 */
	// Search button clicked
	$('#dm_do_search').click(function(){
		MAIL_CONTACT.v_fdm_DoSearchEmail();
	});
	  
	// Check for display button of clear search box
	$('#dm_sl_s_email_keyword').change(function(){
		if ($('#dm_sl_s_email_keyword').val().length > 0) {
			$('#dm_do_search_clr').show();
		}
		else {
			$('#dm_do_search_clr').hide();
		}
	});
	$('#dm_c_sl_list').change(function(){
		$('#dm_sl_list_email .dm_sl-l-body').empty();
	});
	$('#dm_sl_s_email_keyword').keypress(function(){
		if ($('#dm_sl_s_email_keyword').val().length > 0) {
			$('#dm_do_search_clr').show();
		}
		else {
			$('#dm_do_search_clr').hide();
		}
	});	  
	// Clear search box
	$('#dm_do_search_clr').click(function(){
		$('#dm_sl_s_email_keyword').val('').focus();
		$('#dm_do_search_clr').hide();
	});
	// check box status
	$(".dm_sl-l-header div:first span input:checkbox").click(function() {
		var checkedStatus = this.checked;
		$(".dm_sl-l-body .dm_sl-l-row div:nth-child(1) span input:checkbox").each(function() {
		    this.checked = checkedStatus;
		});
	});
	// end script for search khach hang
});